package view.backend.product;

import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import model.BSSingleton;
import model.Product;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;
import view.menu.MenuLabel;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminProduct
{
    private JPanel 
            productPanel,
            prdSearchPanel,
            prdListPanel,
            admNewPanel,
            admNewOverPanel;
    private JButton 
            btnNew,
            btnGoToSearch;
    private AdminProductSearch prdSearch;
    private AdminProductList prdList;
    private AdminNewProduct adminNewProduct;
    private AdminNewProductOverview adminNewProductOverview;
    private model.Product productModel;

    public AdminProduct()
    {
        productModel = new Product();
        
        productPanel = new JPanel(null);
        productPanel.setBounds(95, 320, 900, 500);
        productPanel.setOpaque(false);
        productPanel.addComponentListener(new ComponentAdapter() 
        {
            @Override
            public void componentShown(ComponentEvent evt)
            {
                ArrayList<MenuLabel> menuLabels = ViewSingleton.getSingletonObject().getMenuLabels();
                for (MenuLabel value : menuLabels)
                {
                    if (value.getMenuLabelText().equals("LAGER"))
                    {
                        value.setBackgroundColor();
                    }
                }
                        
                reset();
            }
        });
        
        prdSearch = new AdminProductSearch(this);
        prdSearchPanel = prdSearch.getPanel();
        
        prdList = new AdminProductList(this);
        prdListPanel = prdList.getPanel();
        
        adminNewProduct = new AdminNewProduct(this);
        admNewPanel = adminNewProduct.getPanel();
        
        adminNewProductOverview = new AdminNewProductOverview(this);
        admNewOverPanel = adminNewProductOverview.getPanel();
        
        btnNew = new JButton("ny vare");
        btnNew.setBounds(215, 85, 80, 25);
        btnNew.setBorder(DesignLayout.T_FIELD_RED);
        btnNew.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnNew.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                resetProductModel();
                getAdminNewProduct().setFieldsFromModel();
                prdListPanel.setVisible(false);
                prdSearchPanel.setVisible(false);
                admNewPanel.setVisible(true);
                admNewOverPanel.setVisible(true);
                adminNewProductOverview.updateLabelsFromModel();
                btnNew.setVisible(false);
                btnGoToSearch.setVisible(true);
            }
        });
        if (model.BSSingleton.getSingletonObject().getLoggedInUser() != null)
        {
            if (model.BSSingleton.getSingletonObject().getLoggedInUser().getAccessLevel() > 1)
            {
                btnNew.setVisible(true);
            }
            else
            {
                btnNew.setVisible(false);
            }
        }
        
        btnGoToSearch = new JButton("gå til forside");
        btnGoToSearch.setBounds(290, 265, 150, 25);
        btnGoToSearch.setBorder(DesignLayout.T_FIELD_RED);
        btnGoToSearch.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnGoToSearch.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                prdListPanel.setVisible(true);
                prdSearchPanel.setVisible(true);
                admNewPanel.setVisible(false);
                admNewOverPanel.setVisible(false);
                btnGoToSearch.setVisible(false);
                
                if (model.BSSingleton.getSingletonObject().getLoggedInUser() != null)
                {
                    if (model.BSSingleton.getSingletonObject().getLoggedInUser().getAccessLevel() > 1)
                    {
                        btnNew.setVisible(true);
                    }
                    else
                    {
                        btnNew.setVisible(false);
                    }
                }
            }
        });
        btnGoToSearch.setVisible(false);
        
        productPanel.add(btnGoToSearch);
        productPanel.add(btnNew);
        productPanel.add(prdSearchPanel);
        productPanel.add(prdListPanel);
        productPanel.add(admNewPanel);
        productPanel.add(admNewOverPanel);
        productPanel.setVisible(false);
    }
    
    public void initNewProductCreation()
    {
        prdSearchPanel.setVisible(false);
        prdListPanel.setVisible(false);
        admNewPanel.setVisible(true);
        admNewOverPanel.setVisible(true);
        btnNew.setVisible(false);
        btnGoToSearch.setVisible(true);
    }

    public Product getProductModel()
    {
        return productModel;
    }

    public void setProductModel(Product productModel)
    {
        this.productModel = productModel;
    }
    
    public void resetProductModel()
    {
        productModel = null;
        productModel = new Product();
    }
    
    public void reset()
    {
        resetProductModel();
        getAdminNewProduct().reset();
        getAdminNewProduct().setFieldsFromModel();
        getAdminNewProduct().getPanel().setVisible(false);
        getAdminNewProductOverview().getPanel().setVisible(false);
        getPrdList().reset();
        getPrdList().getPanel().setVisible(true);
        getPrdSearch().getPanel().setVisible(true);
        btnGoToSearch.setVisible(false);
        
        if (model.BSSingleton.getSingletonObject().getLoggedInUser() != null)
        {
            if (model.BSSingleton.getSingletonObject().getLoggedInUser().getAccessLevel() > 1)
            {
                btnNew.setVisible(true);
            }
            else
            {
                btnNew.setVisible(false);
            }
        }
    }
    
    public void setBtnNewVisible(boolean visible)
    {
        btnNew.setVisible(visible);
    }
    
    public void setBtnGoToVisible(boolean visible)
    {
        btnGoToSearch.setVisible(visible);
    }

    public AdminProductSearch getPrdSearch()
    {
        return prdSearch;
    }

    public AdminProductList getPrdList()
    {
        return prdList;
    }

    public AdminNewProduct getAdminNewProduct()
    {
        return adminNewProduct;
    }

    public AdminNewProductOverview getAdminNewProductOverview()
    {
        return adminNewProductOverview;
    }

    public JPanel getPanel()
    {
        return productPanel;
    }    
}
